﻿using System;
using System.Collections;
using System.Collections.Generic;

namespace Iot.TimingDatabase.Models;

/// <summary>
///     表示IoT数据库数据行的记录类型，实现了IReadOnlyList<IotDbColumn />接口
/// </summary>
/// <param name="Timestamp">时间戳</param>
/// <param name="Data">列数据的只读列表</param>
public record IotDbData(long Timestamp, IReadOnlyList<IotDbColumn> Data) : IReadOnlyList<IotDbColumn>
{
    /// <summary>
    ///     实现IEnumerable<IotDbColumn />的GetEnumerator方法
    /// </summary>
    /// <returns>枚举器</returns>
    public IEnumerator<IotDbColumn> GetEnumerator()
    {
        for (var i = 0; i < Count; i++)
        {
            yield return this[i];
        }
    }

    /// <summary>
    ///     实现IEnumerable的GetEnumerator方法
    /// </summary>
    /// <returns>枚举器</returns>
    IEnumerator IEnumerable.GetEnumerator()
    {
        return GetEnumerator();
    }

    /// <summary>
    ///     获取列数
    /// </summary>
    public int Count => Data.Count;

    /// <summary>
    ///     索引器，获取指定索引的列数据
    /// </summary>
    /// <param name="index">列的索引</param>
    /// <returns>对应索引的IotDbColumn对象</returns>
    /// <exception cref="IndexOutOfRangeException">当索引超出范围时抛出</exception>
    public IotDbColumn this[int index] => Count > index ? Data[index] : throw new IndexOutOfRangeException();
}